Skip to content

Conversation

johahor
Copy link
Member

@johahor johahor commented Sep 16, 2026

AI generated tests. Will review myself tomorrow.

One thing i noticed straight away was the placement of the tests, which is different than i'm used to, but apparently that's fairly normal practice in React and Vite projects.

--
Update:
I have now reviewed, look comment below

Closes #24

johahor and others added 4 commits September 16, 2026 21:02
Add vitest, jsdom and Testing Library as dev dependencies and a `test`
block in vite.config.ts (jsdom environment, setup file, mock reset and
readable CSS module class names in snapshots).

src/test/setup.ts registers the jest-dom matchers and cleans up the DOM
and localStorage after each test. src/test/renderWithQueryClient.tsx
renders components inside a QueryClientProvider, and
src/test/fixtures/books.ts holds sample books for component tests.

New scripts: `pnpm test` (run once) and `pnpm test:watch`.

Refs #24

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
- toBook: mapping from an Open Library doc to Book, including fallbacks
  for missing author, year and cover.
- searchBooks: mocks the global fetch with vi.spyOn so no request reaches
  openlibrary.org, checks the query parameters and the error handling.
- useBooks: mocks searchBooks with vi.mock and checks that the hook
  returns mapped books.

Refs #24

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Component tests (props, state and user interaction):
- BookCard: shows book data, calls onBookSelect when clicked
- BookCover: image with alt text, or placeholder without cover
- BookDetails: shows book data, calls onGoBack
- FavoriteButton: toggles aria-pressed and favorites in localStorage
- BookGrid: loading, error and empty states with a mocked searchBooks
- App: selecting a book opens details, going back reuses cached data

Snapshot tests for BookCard, Header and Footer.

Refs #24

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Explain how to run the tests, what is covered by unit, component,
snapshot and integration tests, how requests to Open Library are mocked,
and which browsers and screen sizes the app has been checked in so far.

Refs #24

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
johahor and others added 7 commits September 17, 2026 13:43
Resolve the conflict in vite.config.ts by keeping both the base URL and
CSS module options from main and the Vitest configuration.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
- useBooks now returns { numFound, books }; assert on that shape.
- BookGrid requires page and onPageChange; add tests for the page count
  and for clicking Next.
- Add component tests for Pagination (page info, disabled buttons,
  onPageChange and scroll to top).
- searchBooks: test the new lang, page, sort and limit parameters.
- README: list Pagination under component tests.

Refs #24

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
These tests were generated by AI. Nothing is wrong with them, but having
tests just to get coverage is not necessary and takes the focus away
from the things we should be testing: logic, state and how the parts
work together.

Removed the Header and Footer snapshot tests, the BookCover test and the
useBooks hook test.
BookDetails uses useBookDescription, so its tests render inside a
QueryClientProvider and mock fetchWork with workResponse.json. The
"shows title" test is replaced by one that checks the description loads.

App.test.tsx mocks fetchWork too. Before this the flow test sent a real
request to openlibrary.org in the background and still passed.

Also remove the "2 of 5" Pagination test and the unused bookWithoutCover
fixture, and write down in README what we test and why.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@johahor johahor marked this pull request as ready for review September 18, 2026 10:55
@johahor johahor requested a review from a team as a code owner September 18, 2026 10:55
@johahor
Copy link
Member Author

johahor commented Sep 18, 2026

I removed some of the initial tests that were AI generated (89f098f). Nothing wrong with the tests as the described in the commit as well, but they did not bring any real value.

It took some time to understand this PR, i haven't worked with tests using vite and react before, but i feel like I have a good understanding now.

If you want to learn how it works, i would recommend going into openLibrary.test.tsx file and look at the different tests there. My biggest issue was understanding how this "spy" worked, and why it allowed us to make calls to searchBooks and fetchWork without a network requests:
"const fetchMock = vi
.spyOn(globalThis, "fetch")
.mockResolvedValue(Response.json(searchResponse));"

Once you understand that, the rest of the testing follows normal testing principles, and is fairly straight forward.

So looking at the openLibrary.test.tsx makes you understand how testing the api layer works, and if you want to learn how to test components, i recommend going into either SearchBar.test.tsx, or Pagination.test.tsx. I wrote SearchBar.test.tsx myself to get a feeling on how testing components work (there is a few different user event types that is good to know about).

@johahor johahor added the test label Sep 18, 2026
@johahor johahor changed the title Test/vitest setup test: initial test setup and tests Sep 18, 2026
martakam
martakam previously approved these changes Sep 18, 2026
Copy link
Member

@martakam martakam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me as well. Nice usage of fixtures, snapshots and mocking!

@martakam
Copy link
Member

@hestro Some changes were needed because of the new props for the components BookDetails and BookGrid. Please review whether everything looks good for you as well :)

@martakam martakam requested a review from hestro September 18, 2026 19:31
hestro
hestro previously approved these changes Sep 18, 2026
Copy link
Member

@hestro hestro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good and extensive!

johahor and others added 2 commits September 18, 2026 21:48
SearchBar now requires onFilterChange, which made `pnpm build` fail even
though the tests passed (Vitest does not type-check).

setup.ts also clears sessionStorage between tests, since App reads the
filters from there on mount.

README: BookGrid no longer mocks searchBooks; its tests pass a fake query
result in as a prop.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@johahor
Copy link
Member Author

johahor commented Sep 18, 2026

Two more commits after merging main:

  • e2a4a9e: pnpm build failed on the branch even though pnpm test was green. The filtering PR made onFilterChange a required prop on SearchBar, and SearchBar.test.tsx did not pass it. Vitest does not type-check, so only tsc caught it. Also added sessionStorage.clear() to the test setup, since App now reads filters from sessionStorage on mount, and updated the README mocking section (BookGrid no longer mocks searchBooks; it gets a fake query result as a prop).
  • 8acc2e0: Prettier formatting of useDebounce.ts (tabs to spaces). Unrelated to the tests, but it was the last file failing prettier --check, so the whole repo is clean now.

Lesson for the rest of us: run pnpm build as well as pnpm test before pushing. Tests passing does not mean the types are right.

This comment was generated by Claude Code ^

@hestro hestro self-requested a review September 18, 2026 19:55
Copy link
Member

@hestro hestro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Will make sure to run pnpm build as well

@martakam martakam self-requested a review September 18, 2026 19:59
@martakam martakam merged commit d2c6057 into main Sep 18, 2026
Sign in to join this conversation on GitHub.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

test: set up basic tests with Vitest
3 participants